//
//  UWBrick.m
//  Urban Words
//
//  Created by Tim Brandin on 2011-12-01.
//  Copyright (c) 2011 Sypreme. All rights reserved.
//

#import "UWBrick.h"

static int letterValues[26] = {1,3,3,2,1,4,2,4,1,8,5,1,3,1,1,3,10,1,1,1,1,4,4,8,4,10};

@implementation UWBrick

@synthesize x, y, type, state, letter, ctx;
@synthesize placed, newtile, oldtile, alpha;

- (UWBrick *)initWithCGContextRef:(CGContextRef)_ctx X:(int)_x Y:(int)_y {
    self = [super init];
    if (self) {
        ctx = CGContextRetain(_ctx);
        x = _x;
        y = _y;
        image = [[UIImage imageNamed:@"special"] retain];
        tile = [[UIImage imageNamed:@"tile"] retain];
        hoverImage = [[UIImage imageNamed:@"hover-tile"] retain];
        cropped = NO;
        type = DEFAULT;
        firstTime = YES;
        
        oldtile = NO;
        newtile = NO;
    }
    return self;
}

- (void)render:(CGRect)rect
{
    if (ctx != nil) {
        
        // render brick
        
        if (!cropped) {
            
            CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, 
                                                               CGRectMake(0, type*DISPLAY_BRICK_SIZE, DISPLAY_BRICK_SIZE, DISPLAY_BRICK_SIZE));
            [image release];
            image = [[UIImage imageWithCGImage:imageRef] retain];
            
            CGImageRelease(imageRef);
            
            imageRef = CGImageCreateWithImageInRect(tile.CGImage, 
                                                    CGRectMake(0, 2*DISPLAY_TILE_SIZE + TILE_CROP_Y_OFF, DISPLAY_TILE_SIZE, 
                                                               DISPLAY_TILE_SIZE));
            [tile release];
            tile = [[UIImage imageWithCGImage:imageRef] retain];
            
            CGImageRelease(imageRef);
             
            cropped = YES;
            
        }
        
        CGContextDrawImage(ctx, CGRectMake(x, y, BRICK_SIZE, BRICK_SIZE), image.CGImage);
        
        if (state == HOVER) { // mark tile when hovered
            CGContextDrawImage(ctx, CGRectMake(x, y, BRICK_SIZE, BRICK_SIZE), hoverImage.CGImage);
        }
        
        
        // render tile if set
        
        if (letter) {
            
            if (alpha < 0 && newtile) {
                alpha = 0;
            }
            
            if (alpha >= 0) {
                
                switch (state) {
                    case FAIL:
                        CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:1 green:0 blue:0 alpha:alpha].CGColor);
                        CGContextFillRect(ctx, CGRectMake(x, y, BRICK_SIZE, BRICK_SIZE));
                        break;
                        
                    case PASS:
                        if (newtile) {
                            CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0 green:1 blue:(1-alpha) alpha:1].CGColor);
                        }
                        else if (oldtile) {
                            CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0 green:1 blue:1 alpha:alpha].CGColor);
                        } else {
                            CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0 green:1 blue:0 alpha:alpha].CGColor);
                        }
                        CGContextFillRect(ctx, CGRectMake(x, y, BRICK_SIZE, BRICK_SIZE));
                        break;
                        
                    case MARK:
                        CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:0 green:1 blue:1 alpha:alpha].CGColor);
                        CGContextFillRect(ctx, CGRectMake(x, y, BRICK_SIZE, BRICK_SIZE));
                        break;
                        
                    case OPPONENT:
                        CGContextSetFillColorWithColor(ctx, [UIColor colorWithRed:1 green:1 blue:0 alpha:alpha].CGColor);
                        CGContextFillRect(ctx, CGRectMake(x, y, BRICK_SIZE, BRICK_SIZE));
                        
                    default:
                        break;
                }
            }
            
            if (alpha > 0
                && (!newtile
                    || (newtile && state == PASS)
                    || oldtile)) {
                alpha -= 0.05;
            } else if (!newtile || oldtile) {
                state = NONE;
                alpha = 0;
            }
            
            if (state == FAIL && alpha > 0) {
                alpha -= 0.05;
            } else if (state == FAIL) {
                alpha = 0;
                state = NONE;
            }
            
            CGContextDrawImage(ctx, CGRectMake(x + TILE_X_OFF, y + TILE_Y_OFF, TILE_SIZE, TILE_SIZE), tile.CGImage);
            
            CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
            CGContextSetStrokeColorWithColor(ctx, [UIColor whiteColor].CGColor);
            CGContextSetLineWidth(ctx, 1.0);
            CGContextSetTextDrawingMode(ctx, kCGTextFillStroke);
            CGContextSetShouldSmoothFonts(ctx, true);
            
            // flip cordinates of y
            int y2 = BOARD_SIZE - y - BRICK_SIZE;
            
            CGContextSaveGState(ctx);
            UIGraphicsPushContext(ctx);
            
            CGContextTranslateCTM(ctx, 0.0, BOARD_SIZE);
            CGContextScaleCTM(ctx, 1.0, -1.0);
            
            CGRect textRect = CGRectMake(x - 3, y2 + TILE_TEXT_Y_OFF, TILE_SIZE, TILE_SIZE);
            
            [letter drawInRect:textRect withFont:[UIFont fontWithName:@"Helvetica-Bold" size:36]  lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
            
            CGRect letterValueTextRect = CGRectMake(x - 9, y2 + 1, TILE_SIZE, 15);
            
            NSString *letterValue = [NSString stringWithFormat:@"%i", letterValues[((int)[letter characterAtIndex:0]-65)]];
            
            NSString *font = (type == DL || type == TL ? @"Verdana-Bold" : @"Verdana");
            
            CGContextSetFillColorWithColor(ctx, [UIColor whiteColor].CGColor);
            //CGContextSetLineWidth(ctx, 1.0);
            [letterValue drawInRect:letterValueTextRect withFont:[UIFont fontWithName:font size:11]  lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentRight];
            
            CGContextSetFillColorWithColor(ctx, [UIColor blackColor].CGColor);
            CGContextSetStrokeColorWithColor(ctx, [UIColor clearColor].CGColor);
            
            [letterValue drawInRect:letterValueTextRect withFont:[UIFont fontWithName:font size:11]  lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentRight];
            
            
            UIGraphicsPopContext();
            CGContextRestoreGState(ctx); 
            
        }
        
    }
}

- (void)clear
{
    CGContextClearRect(ctx, CGRectMake(x, y, BRICK_SIZE, BRICK_SIZE));
}

- (void)setState:(BRICK_STATE)input
{
    if (alpha == 0) {
        alpha = 1.0;
    }
    
    if (input == PLACED && type == DEFAULT) {
        type = DF;
    } 
    else if (input == NONE && type == DF) {
        type = DEFAULT;
    }
    
    state = input;
}

- (void)setLetter:(NSString *)input
{
    if ([input length] > 0) {
        if (state != FAIL && state != PASS && state != OPPONENT) 
            self.state = PLAYED;
        
        if (type == DEFAULT) {
            type = DF;
        } 
    } else {
        if (type == DF) {
            type = DEFAULT;
        }
    }
    if (input != letter) {
        [letter release];
        letter = [input copy];
    }
}

- (void)dealloc
{
    [image release];
    [tile release];
    [super dealloc];
}

@end
